<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $row = strtotime(date('Y-m-d').' 23:59:59');
        $date = [];
        for ($i=0; $i < 22; $i++) { 
            $new = strtotime('-'.$i.' Days',$row);
            $time = date('Y-m-d H:i:s',$new);
            $date[] = $time;
        }
        asort($date);
        $date = array_values($date);
        $money = [];
        $count = [];
        for ($i=0; $i < count($date); $i++) { 
            if (isset($date[($i-1)])) {
                $row_money = $this->CommonPayOrderModel->where(['status'=>1])->where('updatetime','between time',[$date[$i-1],$date[($i)]])->sum('money');
                $row_count = $this->CommonPayOrderModel->where(['status'=>1])->where('updatetime','between time',[$date[$i-1],$date[($i)]])->count();
                $money[($date[$i])] = round($row_money,2);
                $count[($date[$i])] = round($row_count,2);
            }
        }
        $arr = [];
        foreach ($money as $k => $v) {
            $arr[(substr($k,0,10))] = $v;
        }
        $money = $arr;
        $arr = [];
        foreach ($count as $k => $v) {
            $arr[(substr($k,0,10))] = $v;
        }
        $count = $arr;

        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $usermoney = model('User')->sum('money');
        $ordermoney = model('PayOrder')->where(['status'=>1])->whereTime('createtime','today')->sum('money');
        $this->view->assign([
            'totaluser'        => $this->CommonUserModel->count(),
            'totalviews'       => round($this->CommonWithdrawModel->where('status!=2')->sum('money'),2),
            'totalorder'       => $this->CommonPayOrderModel->where('status',1)->count(),
            'totalorderamount' => round($this->CommonPayOrderModel->where('status',1)->sum('money'),2),
            'todayqq'          => round($this->CommonPayOrderModel->where('status',1)->where('type','qqpay')->whereTime('updatetime', 'today')->sum('money'),2),
            'yesterdayqq'       => round($this->CommonPayOrderModel->where('status',1)->where('type','qqpay')->whereTime('updatetime', 'yesterday')->sum('money'),2),
            'todayweixin'          => round($this->CommonPayOrderModel->where('status',1)->where('type','wxpay')->whereTime('updatetime', 'today')->sum('money'),2),
            'yesterdayweixin'       => round($this->CommonPayOrderModel->where('status',1)->where('type','wxpay')->whereTime('updatetime', 'yesterday')->sum('money'),2),
            'todayali'          => round($this->CommonPayOrderModel->where('status',1)->where('type','alipay')->whereTime('updatetime', 'today')->sum('money'),2),
            'yesterdayali'       => round($this->CommonPayOrderModel->where('status',1)->where('type','alipay')->whereTime('updatetime', 'yesterday')->sum('money'),2),
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $money,
            'createlist'       => $count,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'usermoney'       => $usermoney,
            'ordermoney'       => $ordermoney,
        ]);

        return $this->view->fetch();
    }

    
    public function transfer(){
        if ($this->request->isPost()) {
            $data = input('post.row/a');
            if (!isset($data['type']) || !isset($data['account']) || !isset($data['money'])) {
                $this->error('请填写完整');
            }
            if (!trim($data['type']) || !trim($data['account']) || !trim($data['money'])) {
                $this->error('请填写完整');
            }
            if ($data['type']!=='wx' && $data['type']!=='ali' && $data['type']!=='qq') {
                $this->error('付款方式出错！');
            }
            $data['money'] = round($data['money'],2);
            if ($data['money'].'' <= 0) {
                $this->error('金额必须为正数');
            }
            if (!$data['realname']) {
                $data['realname'] = '';
            }
            if ($data['type']=='wx') {
                $transfer = WxTransfer($data['account'],$data['realname'],$data['money']);
            }elseif ($data['type']=='ali') {
                $transfer = AliTransfer($data['account'],$data['realname'],$data['money']);
            }else{
                if (!is_numeric($data['account']) || strlen($data['account'])<6 || strlen($data['account'])>10) {
                    return json(['code'=>0,'msg'=>'QQ号码格式错误']);
                }
                $transfer = QQTransfer($data['account'],$data['money'],$data['realname']);
            }
            if (isset($transfer['code']) && $transfer['code']) {
                return json(['code'=>1,'msg'=>'转账成功 [ 单号:'.$transfer['payment_no'].' ]']);
            }else{
                return json(['code'=>0,'msg'=>(isset($transfer['msg'])?$transfer['msg']:'未知错误')]);
            }
        }
        return $this->fetch();
    }

}
